package com.yocard.yxyd.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yocard.yxyd.R;
import com.yocard.yxyd.dto.SettingData;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created on 2019/5/3.
 * 我的设置九宫格
 */
public class SettingAadapter extends RecyclerView.Adapter<SettingAadapter.ViewHolder> {

    int clickPos;
    private List<SettingData> SettingDataList;
    private OnItemClickListener callback;

    public SettingAadapter(List<SettingData> SettingDataList) {

        this.SettingDataList = SettingDataList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_setting, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        if (i == clickPos) {
            viewHolder.itemView.setSelected(true);
        } else {
            viewHolder.itemView.setSelected(false);
        }

        final SettingData itemBean = SettingDataList.get(i);
        viewHolder.ivIcon.setImageResource(itemBean.getResId());
        viewHolder.tvName.setText(itemBean.getName());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onItemClick(itemBean);
            }
        });
    }

    @Override
    public int getItemCount() {
        return SettingDataList == null ? 0 : SettingDataList.size();
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            clickPos = (int) v.getTag();
            notifyDataSetChanged();

        }
    };

    public void setOnItemclick(OnItemClickListener callback) {

        this.callback = callback;
    }

    public interface OnItemClickListener {
        void onItemClick(SettingData settingData);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_icon)
        ImageView ivIcon;
        @BindView(R.id.tv_name)
        TextView tvName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    
}
